//
//  BSSWithdrawalViewController.m
//  PreschoolEducation
//
//  Created by 李传铎 on 2020/11/15.
//  Copyright © 2020 李传铎. All rights reserved.
//

#import "BSSWithdrawalViewController.h"
#import "BSSBankManagementListViewController.h"
#import "BSSViewTool.h"
@interface BSSWithdrawalViewController ()<UITextFieldDelegate>
@property(nonatomic ,strong) UITextField *withdrawalAmountTF;
@property(nonatomic ,strong)UIButton *codeButton;
@property (nonatomic, strong) NSTimer *timer; // 计时器

@property (nonatomic, assign) NSInteger second; // 秒数
@end

@implementation BSSWithdrawalViewController
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    if ([self.timer isValid]) {
        [self.timer invalidate];
    }
    self.timer = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.second = 60;
    // Do any additional setup after loading the view.
    [self creatUI];
    self.view.backgroundColor = colorCode(@"#F9F9F9");
}
- (void)creatUI{
    UIView *bankView = [[UIView alloc]init];
    bankView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bankView];
    [bankView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(__kNavigationBarHeight__);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(65);
    }];
    UIImageView *bankImageV = [[UIImageView alloc]init];
    [bankView addSubview:bankImageV];
    bankImageV.image = imageNameStr(@"ic_bank");
    
    [bankImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(bankView.mas_centerY);
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    
    UILabel *bankNameL = [[UILabel alloc]init];
    bankNameL.textColor = colorCode(@"333333");
    bankNameL.text = @"中国银行 (尾号2873）";
    bankNameL.font = fontCount(16);
    [bankView addSubview:bankNameL];
    
    [bankNameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(bankImageV.mas_right).offset(3);
        make.centerY.mas_equalTo(bankImageV.mas_centerY);
    }];
    
    UIImageView *nextImageV = [[UIImageView alloc]init];
    [bankView addSubview:nextImageV];
    nextImageV.image = imageNameStr(@"ic_right_blod");
    [nextImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(20);
        make.centerY.mas_equalTo(bankImageV.mas_centerY);
        make.width.mas_equalTo(10);
        make.height.mas_equalTo(10);
    }];
    UIButton *bankNextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [bankView addSubview:bankNextBtn];
    [bankNextBtn addTarget:self action:@selector(bankNextClick) forControlEvents:UIControlEventTouchUpInside];
    [bankNextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.and.bottom.mas_equalTo(0);
    }];
    
    UIView *withdrawalAmountView = [[UIView alloc]init];
    [self.view addSubview:withdrawalAmountView];
    withdrawalAmountView.backgroundColor = [UIColor whiteColor];
    
    [withdrawalAmountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(bankView.mas_bottom).offset(16);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(180);
    }];
    UILabel *withdrawalAmountNameL = [[UILabel alloc]init];
    withdrawalAmountNameL.text = @"提现金额";
    withdrawalAmountNameL.textColor = colorCode(@"#000000");
    withdrawalAmountNameL.font = [UIFont boldSystemFontOfSize:16];
    [withdrawalAmountView addSubview:withdrawalAmountNameL];
    
    [withdrawalAmountNameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(18);
    }];
    
    _withdrawalAmountTF = [[UITextField alloc]init];
    [withdrawalAmountView addSubview:_withdrawalAmountTF];
    _withdrawalAmountTF.delegate = self;
    _withdrawalAmountTF.keyboardType = UIKeyboardTypeDecimalPad;
    UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 30, 50)];
    UILabel *leftL = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 30, 50)];
    [leftView addSubview:leftL];
    leftL.textColor = colorCode(@"#141414");
    leftL.font = [UIFont boldSystemFontOfSize:25];
    leftL.text = @"¥";
    _withdrawalAmountTF.leftView = leftView;
    _withdrawalAmountTF.leftViewMode = UITextFieldViewModeAlways;
    _withdrawalAmountTF.textColor = colorCode(@"#141414");
    _withdrawalAmountTF.font = [UIFont boldSystemFontOfSize:25];
    [_withdrawalAmountTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(withdrawalAmountNameL.mas_bottom).offset(10);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(50);
    }];
    UIView *lineView = [[UIView alloc]init];
    [withdrawalAmountView addSubview:lineView];
    lineView.backgroundColor = colorCode(@"#E3E3E4");
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(_withdrawalAmountTF.mas_bottom).offset(8);
        make.height.mas_equalTo(1);
    }];
    
    UILabel *withdrawalAmountL = [[UILabel alloc]init];
    withdrawalAmountL.text = @"可提余额 165.0元，";
    withdrawalAmountL.textColor = colorCode(@"#333333");
    withdrawalAmountL.font = fontCount(14);
    [withdrawalAmountView addSubview:withdrawalAmountL];
    
    [withdrawalAmountL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(lineView.mas_bottom).offset(12);
    }];
    
    UIButton *withdrawalAmountBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [withdrawalAmountView addSubview:withdrawalAmountBtn];
    [withdrawalAmountBtn addTarget:self action:@selector(withdrawalAmountClick) forControlEvents:UIControlEventTouchUpInside];
    [withdrawalAmountBtn setTitle:@"全部提现" forState:0];
    withdrawalAmountBtn.titleLabel.font = fontCount(14);
    [withdrawalAmountBtn setTitleColor:colorCode(@"#4F7BAD") forState:0];
    [withdrawalAmountBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(withdrawalAmountL.mas_centerY);
        make.left.mas_equalTo(withdrawalAmountL.mas_right).offset(2);
        make.height.mas_equalTo(25);
        make.width.mas_equalTo(60);
    }];
    UILabel *detailL = [[UILabel alloc]init];
    [withdrawalAmountView addSubview:detailL];
    detailL.font = fontCount(12);
    detailL.textColor = colorCode(@"666666");
    detailL.text = @"收取服务费2元/笔";
    
    [detailL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(withdrawalAmountL.mas_bottom).offset(12);
    }];
    UILabel *mobileNameL = [[UILabel alloc]init];
    [self.view addSubview:mobileNameL];
    mobileNameL.text = @"验证当前绑定的手机号";
    mobileNameL.textColor = colorCode(@"#999999");
    mobileNameL.font = fontCount(12);
    [mobileNameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(withdrawalAmountView.mas_bottom);
        make.height.mas_equalTo(52);
    }];
    
    UILabel *mobileL = [[UILabel alloc]init];
    [self.view addSubview:mobileL];
    mobileL.text = @"134****1234";
    mobileL.font = fontCount(12);
    mobileL.textColor = colorCode(@"333333");
    
    [mobileL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(mobileNameL.mas_right).offset(12);
        make.centerY.mas_equalTo(mobileNameL.mas_centerY);
    }];
    
    UIView *codeView = [[UIView alloc]init];
    [self.view addSubview:codeView];
    codeView.backgroundColor = [UIColor whiteColor];
    
    [codeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(mobileNameL.mas_bottom);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(49);
    }];
    
    UIImageView *codeImageV = [[UIImageView alloc]init];
    [codeView addSubview:codeImageV];
    codeImageV.image = imageNameStr(@"ic_yanzhengma");
    [codeImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(16);
        make.height.mas_equalTo(18);
        make.left.mas_equalTo(24);
        make.centerY.mas_equalTo(codeView.mas_centerY);
    }];
    UITextField *codeTF = [[UITextField alloc]init];
    [codeView addSubview:codeTF];
    codeTF.placeholder = @"请输入验证码";
    codeTF.textColor = colorCode(@"333333");
    [codeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(codeImageV.mas_right).offset(5);
        make.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(200);
    }];
    [codeView addSubview:self.codeButton];
    
    UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:submitBtn];
    [submitBtn setBackgroundImage:imageNameStr(@"BSS_submit_bg") forState:0];
    [submitBtn setTitle:@"提现" forState:0];
    submitBtn.titleLabel.font = fontCount(18);
    [submitBtn setTitleColor:[UIColor whiteColor] forState:0];
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(codeView.mas_bottom).offset(31);
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.width.mas_equalTo(345);
        make.height.mas_equalTo(48);
    }];
}
#pragma mark - 按钮点击

- (void)codeAction:(UIButton *)sender {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
                   [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
                   
                   NSString* string = [NSString stringWithFormat:@"%lds",(long)self.second];
                   [sender setTitle:string forState:UIControlStateDisabled];
                   [self.timer fire];
//                   [self.codeTextfield becomeFirstResponder];
//    BSSFileManager *fileManager = [BSSFileManager shareBSSFileManager];
//    NSDictionary *userInfoDict = [fileManager readUserInformationFile];
//
//    BSSLoginViewModel * viewModel = [[BSSLoginViewModel alloc] init];
//    if ([self checkPhone]) {
//        [viewModel checkPhoneNoWithParam:@{
//                                           @"mobile" : [self.phoneNoTextfield.text stringByReplacingOccurrencesOfString:@" " withString:@""],
//                                           @"oldMobile" : [userInfoDict objectForKey:@"mobile"] ? : @""
//                                           } success:^(id  _Nonnull returnValue) {
//
//           self.messageLabel.attributedText = [[NSAttributedString alloc] initWithString:@""];
//            sender.enabled = NO;
//            WEAKSELF
//            [viewModel getModifyMobileValidateCode:@{
//                                                  @"mobile" : [self.phoneNoTextfield.text stringByReplacingOccurrencesOfString:@" " withString:@""]
//                                                  } success:^(id  _Nonnull returnValue) {
//                weakSelf.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
//                [[NSRunLoop currentRunLoop] addTimer:weakSelf.timer forMode:NSRunLoopCommonModes];
//
//                NSString* string = [NSString stringWithFormat:@"%lds",(long)weakSelf.second];
//                [sender setTitle:string forState:UIControlStateDisabled];
//                [weakSelf.timer fire];
//                [weakSelf.codeTextfield becomeFirstResponder];
//            } failure:^(NSInteger errorCode) {
//                sender.enabled = YES;
//            }];
//        } failure:^(id  _Nonnull responseObject) {
//            [self configMessageLabel:@""];
//            if ([responseObject[@"code"] integerValue] == 2) {
//                [self configMessageLabel:@"该手机号已注册"];
//            } else {
//                if (responseObject[@"message"]) {
//                    [viewModel showHUDText:responseObject[@"message"]];
//                }
//            }
//        }];
//    }
}
#pragma mark - 计时器
- (void)timerAction:(NSTimer *)timer {
    self.second --;
    NSString* string = [NSString stringWithFormat:@"%lds",(long)_second];
    [self.codeButton setTitle:string forState:UIControlStateNormal];
    if (self.second == 0) {
        if ([timer isValid]) {
            [timer invalidate];
            timer = nil;
        }
        [self.codeButton setTitle:@"重新获取" forState:UIControlStateNormal];
        self.second = 60;
        self.codeButton.enabled = YES;
    }
}
- (void)withdrawalAmountClick{
    
}
//添加银行卡的跳转
- (void)bankNextClick{
    BSSBankManagementListViewController *vc = [[BSSBankManagementListViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (UIButton *)codeButton {
    if (!_codeButton) {
        _codeButton = [BSSViewTool createButtonWithFrame:CGRectMake(SCREEN_WIDTH - 100, 8.5, 85, 28) title:@"获取验证码" titleFont:fontCount(15) titleColor:colorCode(@"#4F7BAD") normaleBackgroundImage:nil highlightBackgroundImage:nil normalImage:nil highlightImage:nil backgroundColor:nil actionTarget:self sel:@selector(codeAction:)];
//        UIImage *image1 = [UIImage createImageWithColor:__kColorAlpha(138, 181, 96, 1)  size:CGSizeMake(__kScreenWidth__ - 46, 44)];
//        UIImage *image2 = [UIImage createImageWithColor:__kColorAlpha(138, 181, 96, 0.6)  size:CGSizeMake(__kScreenWidth__ - 46, 44)];
//
//        [_codeButton setBackgroundImage:image1 forState:UIControlStateNormal];
//        [_codeButton setBackgroundImage:image2 forState:UIControlStateDisabled];
        _codeButton.clipsToBounds = YES;
    }
    return _codeButton;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([string isEqualToString:@""])
    {
        return YES;
    }
    if(textField.text.length >= 6){
        return NO;
    }
    return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
